#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qdebug.h"

#define MODBUS_DRIVER_DEVICE_FILE_PATH "/tcrwork/linuxController/trunk/TcrController/data/globalConfig/modbusRtuDevice/modbusDriverDeviceInfo.ini"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    driver=NULL;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    if(NULL==driver)
    {
        driver=new Modbus485Driver(MODBUS_DRIVER_DEVICE_FILE_PATH,ui->lineEdit->text().toInt());
        if(1==driver->connectPort())
        {
            qDebug()<<"add device and connect sucess";
        }
        else
        {
            qDebug()<<"connect failed";
        }
    }
    else
    {
        qDebug()<<"add device failed";
        return ;
    }
    //add device
//    if(1==driver->addDriver(ui->lineEdit->text().toInt()))
//    {
//        qDebug()<<"add device sucess";
//    }
//    else
//    {
//        qDebug()<<"add device failed";
//    }
}

void MainWindow::on_pushButton_2_clicked()
{
    //set pwm
    if(1==driver->setPwm(ui->lineEdit_2->text().toInt()))
    {
        qDebug()<<"set pwm sucess";
    }
    else
    {
        qDebug()<<"set pwm failed";
    }

}

void MainWindow::on_pushButton_3_clicked()
{
    //set max currency
    if(1==driver->setMaxCurrency(ui->lineEdit_3->text().toInt()))
    {
        qDebug()<<"set max currency sucess";
    }
    else
    {
        qDebug()<<"set max currency failed";
    }
}

void MainWindow::on_pushButton_4_clicked()
{
    //set ratedCurrency
    if(1==driver->setRatedCurrency(ui->lineEdit_4->text().toInt()))
    {
        qDebug()<<"set ratedCurrency sucess";
    }
    else
    {
        qDebug()<<"set ratedCurrency failed";
    }
}

void MainWindow::on_pushButton_5_clicked()
{
    //set mode
    if(1==driver->setDriverMode(ui->lineEdit_5->text().toInt()))
    {
        qDebug()<<"set mode sucess";
    }
    else
    {
        qDebug()<<"set mode failed";
    }
}

void MainWindow::on_pushButton_6_clicked()
{
    int errorCode;
    double speedFeedback;
    double currencyFeedback;
    int positionFeedback;
    driver->getErrorCode(errorCode);
    ui->lineEdit_6->setText(QString::number(errorCode));
    driver->getSpeedFeedback(speedFeedback);
    ui->lineEdit_7->setText(QString::number(speedFeedback));
    driver->getCurrencyFeedback(currencyFeedback);
    ui->lineEdit_8->setText(QString::number(currencyFeedback));
    driver->getPositionFeedback(positionFeedback);
    ui->lineEdit_9->setText(QString::number(positionFeedback));
}

void MainWindow::on_pushButton_7_clicked()
{
    //set velocity
    if(1==driver->setSpeed(ui->lineEdit_10->text().toInt()))
    {
        qDebug()<<"set velocity sucess";
    }
    else
    {
        qDebug()<<"set velocity failed";
    }
}
